﻿using System.Collections;
using System.Linq;

namespace Lottery.Classes
{
    public interface ISwap
    {
        void Swap(int[] array, int left, int right);
    }
    public class Sort : ISwap, IComparer
    {
        public static IComparer comparer;
        public static ISwap swapper;
        public static void QuickSort(int[] array)
        {
            Sort s = new Sort();
            Sort.comparer = s;
            Sort.swapper = s;
            QuickSort(array, 0, array.Length - 1);
        }
        public static void QuickSort(int[] array, ISwap swapper)
        {
            Sort.comparer = new Sort();
            Sort.swapper = swapper;
            QuickSort(array, 0, array.Length - 1);
        }
        public static void QuickSort(int[] array, IComparer comparer)
        {
            Sort.comparer = comparer;
            Sort.swapper = new Sort();
            QuickSort(array, 0, array.Length - 1);
        }
        public static void QuickSort(int[] array, IComparer comparer, ISwap swapper)
        {
            Sort.comparer = comparer;
            Sort.swapper = swapper;
            QuickSort(array, 0, array.Length - 1);
        }
        private static void QuickSort(int[] array, int lower, int upper)
        {
            if (lower < upper)
            {
                int split = Pivot(array, lower, upper);
                QuickSort(array, lower, split - 1);
                QuickSort(array, split + 1, upper);
            }
        }
        private static int Pivot(int[] array, int lower, int upper)
        {
            int left = lower + 1;
            object pivot = array[lower];
            int right = upper;
            while (left <= right)
            {
                while ((left <= right) && (comparer.Compare(array[left], pivot) <= 0))
                {
                    ++left;
                }
                while ((left <= right) && (comparer.Compare(array[right], pivot) > 0))
                {
                    --right;
                }
                if (left < right)
                {
                    swapper.Swap(array, left, right);
                    ++left;
                    --right;
                }
            }
            swapper.Swap(array, lower, right);
            return right;
        }
        public void Swap(int[] array, int left, int right)
        {
            int swap = array[left];
            array[left] = array[right];
            array[right] = swap;
        }
        public int Compare(object a, object b)
        {
            return a.ToString().CompareTo(b.ToString())*-1;
        }
    }
}
